# -*- coding:utf-8 -*-
from flask import  render_template, Blueprint,session
from sqlalchemy import not_

from apps.models import Kind, News

index = Blueprint("index", __name__)


@index.route('/')
def home():
    user = session.get('user')
    # 查询所有种类
    kind_list = Kind.query.all()
    # 热门新闻
    hot_news = News.query.order_by("-click_num")[0:4]
    # 新闻
    news = Kind.query.filter_by(type="新闻").first()
    news_list = News.query.filter_by(type_id=news.type_id).order_by("-create_time")[0:9]
    # 娱乐新闻
    recreation = Kind.query.filter_by(type="娱乐").first()
    recreation_list = News.query.filter_by(type_id=recreation.type_id).order_by("-create_time")[0:9]
    # 体育新闻
    sports = Kind.query.filter_by(type="体育").first()
    sports_list = News.query.filter_by(type_id=sports.type_id).order_by("-create_time")[0:9]
    # 图片新闻
    img_news = News.query.filter(not_(News.img_url == '0'))[0:5]
    # 财经新闻
    finance = Kind.query.filter_by(type="财经").first()
    finance_list = News.query.filter_by(type_id=finance.type_id).order_by("-create_time")[0:9]
    # 科技新闻
    science = Kind.query.filter_by(type="科技").first()
    science_list = News.query.filter_by(type_id=science.type_id).order_by("-create_time")[0:9]
    # 汽车新闻
    car = Kind.query.filter_by(type="汽车").first()
    car_list = News.query.filter_by(type_id=science.type_id).order_by("-create_time")[0:9]
    # 女人新闻
    woman = Kind.query.filter_by(type="女人").first()
    woman_list = News.query.filter_by(type_id=woman.type_id).order_by("-create_time")[0:9]
    # 房产新闻
    house = Kind.query.filter_by(type="房产").first()
    house_list = News.query.filter_by(type_id=house.type_id).order_by("-create_time")[0:9]
    # 旅游新闻
    travel = Kind.query.filter_by(type="旅游").first()
    travel_list = News.query.filter_by(type_id=travel.type_id).order_by("-create_time")[0:9]
    # 最新消息
    now_news = News.query.order_by("-create_time")[0:9]
    # 教育新闻
    education = Kind.query.filter_by(type="教育").first()
    education_list = News.query.filter_by(type_id=education.type_id).order_by("-create_time")[0:12]
    # 公益新闻
    public = Kind.query.filter_by(type="公益").first()
    public_list = News.query.filter_by(type_id=public.type_id).order_by("-create_time")[0:12]
    # 校园新闻
    school = Kind.query.filter_by(type="校园").first()
    school_list = News.query.filter_by(type_id=school.type_id).order_by("-create_time")[0:9]
    # 轮播新闻
    banner_list = News.query.filter(not_(News.img_url == '0')).order_by("-create_time")[0:3]
    return render_template('index/index.html', kind_list=kind_list, news=news, news_list=news_list, hot_news=hot_news,
                           recreation_list=recreation_list, recreation=recreation, sports=sports,
                           sports_list=sports_list, img_news=img_news, finance=finance, finance_list=finance_list,
                           science=science, science_list=science_list, car=car, car_list=car_list, woman=woman,
                           woman_list=woman_list, house=house, house_list=house_list, travel=travel,
                           travel_list=travel_list, now_news=now_news, education=education,
                           education_list=education_list,banner_list=banner_list,
                           public=public, public_list=public_list, school=school, school_list=school_list,user=user)
